﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Application.Model;
using Domain.Model.Entidades.Comparendos;
using Infrastructure.Base.InDatabase;
using Domain.Model.Entidades.Infractors;
using Domain.Model.Infractores;
using Domain.Model.Comparendos;
using Domain.Model.Entidades.Vehiculos;
using Domain.Model.Vehiculos;
using Domain.Model.Infracciones;
using Domain.Model.SecretariasDeTransito;
using Domain.Model.Agentes;
using Domain.Model.Direcciones;
using Domain.Model.Aggregates;

namespace Application.WebServices
{
    /// <summary>
    /// Summary description for ServicioInfracciones
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ServicioInfracciones : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public List<ComparendoDTO> getComparendosInfractor(string numIdentificacion)
        {
            //repositorios y servicios
            BComparendoRepository bCompRepo = new BComparendoRepository();
            ComparendoService servicioComparendos = new ComparendoService(bCompRepo);

            BInfractorRepository bInfracRepo = new BInfractorRepository();
            InfractorService servicioInfractores = new InfractorService(bInfracRepo);

            BVehiculoRepository bVehicRepo = new BVehiculoRepository();
            VehiculoService servicioVehiculos = new VehiculoService(bVehicRepo);

            //Obtener el id infractor a partir de la identificación
            InfractorId idInfractor = servicioInfractores.FromIdentificacion(numIdentificacion);

            //obtener los comparendos del infractor
            List<Comparendo> listaComparendos = servicioComparendos.FromIdInfractor(idInfractor);

            //transformar las entidades comparendos al objeto DTO
            List<ComparendoDTO> listaComparendosDTO = new List<ComparendoDTO>();

            foreach (var item in listaComparendos)
            {
                ComparendoDTO newComparendoDTO = new ComparendoDTO();
                
                //Datos del Comparendo
                newComparendoDTO.Numero = item.Numero;
                newComparendoDTO.Fecha = item.Fecha;
                newComparendoDTO.EstadoComparendo = item.Estado;

                //Obtener datos del infractor
                Infractor infractor = servicioInfractores.InfractorFromId(idInfractor.Value.ToString());
                newComparendoDTO.NombresInfractor = infractor.NombresCompleto;
                newComparendoDTO.IdentificacionInfractor = infractor.NumIdentificacion;

                //Datos del vehículo
                Vehiculo vehiculo = servicioVehiculos.VehiculoFromId(item.VehiculoId.Value.ToString());
                newComparendoDTO.PlacaVehiculo = vehiculo.Placa;


                listaComparendosDTO.Add(newComparendoDTO);
            }

            return listaComparendosDTO;
        }


        [WebMethod]
        public void GuardarComparendo(string numero,
                DateTime fecha,
                string tipoInfractor,
                string estado,
                string observacion,
            SecretariaDeTransitoId secretariaTransitoId,
            VehiculoId vehiculoId,
            InfraccionId infraccionId,
            AgenteId agenteId,
            InfractorId infractorId,
            Direccion direccion)
        {
            BComparendoRepository bCompRepo = new BComparendoRepository();
            Comparendo comparendo=ComparendoAggregate.CreateComparendo((ComparendoId)bCompRepo.NextIdentity(),numero, fecha, tipoInfractor, estado, observacion, secretariaTransitoId,
                                                        vehiculoId, infraccionId, agenteId, infractorId, direccion );

            bCompRepo.Insert(comparendo);
        }
            

        [WebMethod]
        public List<ComparendoDTO> AllComparendos()
        {

            //repositorios y servicios
            BComparendoRepository bCompRepo = new BComparendoRepository();
            ComparendoService servicioComparendos = new ComparendoService(bCompRepo);

            BInfractorRepository bInfracRepo = new BInfractorRepository();
            InfractorService servicioInfractores = new InfractorService(bInfracRepo);

            BVehiculoRepository bVehicRepo = new BVehiculoRepository();
            VehiculoService servicioVehiculos = new VehiculoService(bVehicRepo);


            ComparendoService comparendoService = new ComparendoService(new BComparendoRepository());
            List<Comparendo> listaComparendos = comparendoService.AllComparendos();


            //transformar las entidades comparendos al objeto DTO
            List<ComparendoDTO> listaComparendosDTO = new List<ComparendoDTO>();

            foreach (var item in listaComparendos)
            {
                ComparendoDTO newComparendoDTO = new ComparendoDTO();

                //Datos del Comparendo
                newComparendoDTO.Numero = item.Numero;
                newComparendoDTO.Fecha = item.Fecha;
                newComparendoDTO.EstadoComparendo = item.Estado;

                //Obtener datos del infractor
                Infractor infractor = servicioInfractores.InfractorFromId(item.InfractorId.Value.ToString());
                newComparendoDTO.NombresInfractor = infractor.NombresCompleto;
                newComparendoDTO.IdentificacionInfractor = infractor.NumIdentificacion;

                //Datos del vehículo
                Vehiculo vehiculo = servicioVehiculos.VehiculoFromId(item.VehiculoId.Value.ToString());
                newComparendoDTO.PlacaVehiculo = vehiculo.Placa;


                listaComparendosDTO.Add(newComparendoDTO);
            }

            return listaComparendosDTO;
        }

    }
}
